package com.rtos.model.fileSystem;

import com.rtos.utils.Util;

import java.io.*;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @Author: biubiu0002
 * @Date: 2021-11-05 11:42
 **/
public class FileSystem {
    private static FileSystem fileSystem;
    private FileUtil fileUtil = FileUtil.getInstance();

    private FileSystem(){
        init();
    }

    public static FileSystem getInstance(){
        if(fileSystem==null){
            fileSystem= new FileSystem();
        }
        return fileSystem;
    }

    public void init(){
        try {
            this.loadExeList();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<String> loadExeFileContentFromJavaIO(){
        List<String> exeFileList = new ArrayList<>();
        try {
            //由于jar包中存放着resources文件导致的bug 只能手动创建

            exeFileList.add(streamToString("t1.exe"));
            exeFileList.add(streamToString("t2.exe"));
            exeFileList.add(streamToString("t3.exe"));
            exeFileList.add(streamToString("t4.exe"));
            exeFileList.add(streamToString("t5.exe"));
            exeFileList.add(streamToString("t6.exe"));
            exeFileList.add(streamToString("t7.exe"));
            exeFileList.add(streamToString("t8.exe"));
            exeFileList.add(streamToString("t9.exe"));
            exeFileList.add(streamToString("t10.exe"));



        } catch (Exception e) {
            e.printStackTrace();
        }
        return exeFileList;

    }

    private String streamToString(String path){
        InputStream resourceAsStream = getClass().getResourceAsStream(path);
        StringBuilder sb = new StringBuilder();

        try {
            byte[] bytes = new byte[4096];
            while (resourceAsStream.read(bytes) != -1) {
                sb.append(new String(bytes,"utf-8"));
            }
            return sb.toString();

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;




    }

    private boolean loadExeList() throws Exception {
        List<String> exeFileList = this.loadExeFileContentFromJavaIO();
        String dirOnePath = "d1";
        String dirOnePath2 = "d1/d3";
        String dirOnePath3 = "d1/d3/d4";
//        String fileOnePath = "Rme";
        String fileOnePath1 = "e1.exe";
        String fileOnePath2 = "e2.exe";
        String fileOnePath3 = "e3.exe";
        String fileOnePath4 = "e4.exe";
        String fileOnePath5 = dirOnePath2+"/e5.exe";
        String fileOnePath6 = dirOnePath2+"/e6.exe";
        String fileOnePath7 = dirOnePath3+"/e7.exe";
        String fileOnePath8 = dirOnePath3+"/e8.exe";
        String fileOnePath9 = dirOnePath3+"/e9.exe";
        String fileOnePath10 = dirOnePath3+"/e10.exe";
        String context1 = exeFileList.get(0);
        String context2 = exeFileList.get(1);
        String context3 = exeFileList.get(2);
        String context4 = exeFileList.get(3);
        String context5 = exeFileList.get(4);
        String context6 = exeFileList.get(5);
        String context7 = exeFileList.get(6);
        String context8 = exeFileList.get(7);
        String context9 = exeFileList.get(8);
        String context10 = exeFileList.get(9);



        fileUtil.makeDirectory(dirOnePath);
        fileUtil.makeDirectory(dirOnePath2);
        fileUtil.makeDirectory(dirOnePath3);

        fileUtil.createFile(fileOnePath1, context1);
        fileUtil.createFile(fileOnePath2, context2);
        fileUtil.createFile(fileOnePath3, context3);
        fileUtil.createFile(fileOnePath4, context4);
        fileUtil.createFile(fileOnePath5, context5);
        fileUtil.createFile(fileOnePath6, context6);
        fileUtil.createFile(fileOnePath7, context7);
        fileUtil.createFile(fileOnePath8, context8);
        fileUtil.createFile(fileOnePath9, context9);
        fileUtil.createFile(fileOnePath10, context10);
        return true;

    }

//    public boolean
}
